﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;

namespace SatisGonderim
{
    public class Satis
    {
        public string ID { get; set; }
    }

    [Serializable()]
    public class Database : ISerializable
    {
        Dictionary<string, string> _stoklar = new Dictionary<string, string>();

        public Dictionary<string, string> Stoklar { get { return _stoklar; } set { _stoklar = value; } }
        public Database() { }
        public Database(SerializationInfo info, StreamingContext ctxt)
        {
            Stoklar = (Dictionary<string, string>)info.GetValue("Stoklar", typeof(Dictionary<string, string>));
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Stoklar", this.Stoklar);
        }
    }

    public class Functions
    {
        public static Byte[] Serialize(Database db)
        {
            MemoryStream mem = new MemoryStream();
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(mem, db);
            mem.Close();
            return mem.ToArray();
        }

        public static Database DeSerialize(Byte[] data)
        {
            Database db = null;
            MemoryStream mem = new MemoryStream(data);
            BinaryFormatter bformatter = new BinaryFormatter();
            db = (Database)bformatter.Deserialize(mem);
            mem.Close();
            return db;
        }

        public static Database VtYukle()
        {
            Database db = null;
            try
            {
                if (File.Exists(Application.StartupPath + "\\DB.bin"))
                {
                    byte[] data = File.ReadAllBytes(Application.StartupPath + "\\DB.bin");
                    db = DeSerialize(data);
                }
                else
                    db = new Database();
            }
            catch (Exception)
            {

            }
            return db;
        }

        public static void SaveDb(Database db)
        {
            byte[] data = Serialize(db);
            File.WriteAllBytes(Application.StartupPath + "\\DB.bin", data);
        }

    }
}
